/*
 * HandleDrawer.java
 * classes : jh.widget.HandleDrawer
 * @author 梁天刚
 * V 1.0.0
 * Create at 2011-9-21 下午2:28:22
 * 用到的配置文件是 attrs_handledrawer.xml
 */
package com.wzz.common.widget;

import com.hymobile.common.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.widget.SlidingDrawer;

/**
 * jh.widget.HandleDrawer
 * @author 梁天刚 <br/>
 * create at 2011-9-21 下午2:28:22
 */
public class HandleDrawer extends SlidingDrawer {
  private int handlePosition;
  private final int HANDLE_CENTER = 0;
  private final int HANDLE_LEFT = 1;
  private final int HANDLE_RIGHT = 2;
  private final int HANDLE_TOP = 3;
  private final int HANDLE_BOTTOM = 4;
  
  public HandleDrawer(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }
  
  public HandleDrawer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HandleDrawer);
    handlePosition = a.getInt(R.styleable.HandleDrawer_handlePosition, HANDLE_CENTER);
    a.recycle();
  }
  
  protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);

    final int width = r - l;
    final int height = b - t;
    
    final View handle = getHandle();
    int left = handle.getLeft();
    int top = handle.getTop();
    int childWidth = handle.getMeasuredWidth();
    int childHeight = handle.getMeasuredHeight();
    if(handlePosition == HANDLE_LEFT){
      handle.layout(0, top, childWidth, top + childHeight);
    }else if(handlePosition == HANDLE_RIGHT){
      handle.layout(width - childWidth, top, width, top + childHeight);
    }else if(handlePosition == HANDLE_TOP){
      handle.layout(left, 0, left + childWidth, childHeight);
    }else if(handlePosition == HANDLE_BOTTOM){
      handle.layout(left, height - childHeight, left + childWidth, height);
    }
  }

}
